package org.todo.controller;


import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;

import org.springframework.web.portlet.mvc.AbstractController;
import org.springframework.web.portlet.bind.PortletRequestUtils;
import org.todo.service.CategoryService;

/**
 * Controller that handles the delete action requests for categories
 * @author Matthias Braunhofer
 */
public class DeleteCategoryController extends AbstractController {
    //injected dependency
    private CategoryService categoryService;

    /**
     * Handles the delete action request for a certain category
     * @param request
     * @param response
     * @throws java.lang.Exception
     */
    @Override
    protected void handleActionRequestInternal(ActionRequest request, ActionResponse response) throws Exception {
        int categoryId = PortletRequestUtils.getIntParameter(request, "categoryId");
        categoryService.deleteCategory(categoryId);

        //remove session attribute and map rendering phase to todosController
        request.getPortletSession().removeAttribute("selectedCategory");
        response.setRenderParameter("action", "todos");
    }
  
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }
}
